package com.rookieci.totp.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;

import com.rookieci.totp.R;
import com.rookieci.totp.TotpApp;
import com.rookieci.totp.adapter.QrCodeAdapter;
import com.rookieci.totp.db.QRCodeDB;
import com.rookieci.totp.mode.QRCodeMode;
import com.rookieci.widget.SlideRecyclerView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * Created by 磁磁帅 on 2025/8/10 0010.
 * 功能：
 */
public class QrCodeListActivity extends Activity implements QrCodeAdapter.OnAdapterListener {
    private SlideRecyclerView recyclerView;
    private List<QRCodeMode> modeList = new ArrayList<>();
    private QrCodeAdapter adapter;
    private boolean isDestroy;

    public static void open(Activity activity) {
        Intent intent = new Intent(activity, QrCodeListActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode_list);

        recyclerView = findViewById(R.id.recyclerView);
        adapter = new QrCodeAdapter(this, modeList).setOnAdapterListener(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        this.loadListData();
    }

    @Override
    protected void onDestroy() {
        isDestroy = true;
        super.onDestroy();
    }

    private void loadListData() {
        TotpApp.runThread(() -> {
            List<QRCodeMode> codeModeList = new QRCodeDB().selectAll();
            runOnUiThread(() -> pullData(codeModeList));
        });
    }

    //加载数据
    private void pullData(List<QRCodeMode> list) {
        if (isDestroy || list == null || list.size() == 0) return;
        modeList.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onAdapterClick(QRCodeMode mode) {
        QrCodeTextActivity.open(this, mode.codeStr);
    }

    private AlertDialog btnConfirmDialog;

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onDelClick(QRCodeMode mode) {
        if (btnConfirmDialog != null) btnConfirmDialog.dismiss();
        btnConfirmDialog = new AlertDialog.Builder(this).setTitle("提示").setMessage("确定删除?").setPositiveButton("确定", (dialog, which) -> {
            int index = modeList.indexOf(mode);
            if (index < 0) {
                modeList.remove(index);
                adapter.notifyDataSetChanged();
            } else {
                modeList.remove(mode);
                adapter.notifyItemRemoved(index);
            }
            //删除数据库数据
            new QRCodeDB().delect(mode._id);
        }).setNegativeButton("再想想", null).create();
        btnConfirmDialog.setOnDismissListener(dialog -> {
            if (dialog == btnConfirmDialog) btnConfirmDialog = null;
        });
        btnConfirmDialog.show();
    }
}
